<template>
  <div id="ranking-board">
    <div class="ranking-board-title">高频词统计</div>
    <dv-scroll-ranking-board :config="config" />
  </div>
</template>

<script>
export default {
  name: "RankingBoard",
  props: ["data"],
  data() {
    return {
      all_data: [],
      config: {
        data: [],
        rowNum:9,
      },
    };
  },
  methods: {
    updateData(row) {
      let init_data = this.all_data.slice();
      let words = row.words;
        for (let k = 0; k < words.length; k++) {
          let have = false;
          for (let j = 0; j < init_data.length; j++) {
            if (init_data[j].name == words[k]) {
              init_data[j].value++;
              have = true;
              break;
            }
          }
          if (!have) init_data.push({ name: words[k], value: 1 });
        }
        this.all_data = init_data;
        this.all_data.sort(this.sortNumber);
        this.config.data = this.all_data.slice(0,this.config.rowNum);
      this.config = {...this.config};
    },
    sortNumber(a, b) {
      return b.value-a.value;
    },
    createData() {
      let init_data = [];
      for (let i = 0; i < this.data.length; i++) {
        let words = this.data[i].words;
        for (let k = 0; k < words.length; k++) {
          let have = false;
          for (let j = 0; j < init_data.length; j++) {
            if (init_data[j].name == words[k]) {
              init_data[j].value++;
              have = true;
              break;
            }
          }
          if (!have) init_data.push({ name: words[k], value: 1 });
        }
      }
      this.all_data = init_data;
      this.all_data.sort(this.sortNumber);
     
      this.config.data = this.all_data.slice(0,this.config.rowNum);
      this.config = {...this.config};
    },
  },
  mounted() {
    this.createData();
  },
};
</script>

<style lang="less">
#ranking-board {
  width: 20%;
  box-shadow: 0 0 3px blue;
  display: flex;
  flex-direction: column;
  background-color: rgba(6, 30, 93, 0.5);
  border-top: 2px solid rgba(1, 153, 209, 0.5);
  box-sizing: border-box;
  padding: 0px 30px;

  .ranking-board-title {
    font-weight: bold;
    height: 50px;
    display: flex;
    align-items: center;
    font-size: 20px;
  }

  .dv-scroll-ranking-board {
    flex: 1;
  }
}
</style>
